package A.动态数组;

public class _121_买卖股票的最佳时机 {
    /**
     * 自解-暴力：最大利润 max-min 并且index要逐渐增大
     *
     */
        public int maxProfit(int[] prices) {

            int get =0;
            for (int i=0;i<prices.length-1;i++){
                for (int j = i+1;j<prices.length;j++){
                    if (get <(prices[j] - prices[i])){
                        get = prices[j] - prices[i];
                    }


                }
            }
            return get;
        }

    /**
     * 自己优化：---不会了
     * 题解：动态更新最小和最大利润
     * @param prices
     * @return
     */

    public int maxProfit1(int[] prices) {
        int min = Integer.MAX_VALUE;
        int maxProfit = 0;
        for (int i=0;i<prices.length;i++){
            if (prices[i]<min){
                min = prices[i];
            }else if (prices[i]-min > maxProfit){
                maxProfit = prices[i]-min;
            }

        }
        return maxProfit;
    }

    }
